package com.xxxt.utils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import javax.servlet.http.HttpServletRequest;

import com.xxxt.entity.Practice;
import com.xxxt.entity.Student;

/**
 * @description:
 * @author:wsc
 * @date:2020年6月11日 下午10:54:36
 */
public class PracticeTestifyUtil {

	//证明材料文件夹
	public static final String testifyPath = "testify";
	//证明材料临时文件夹
	public static final String temporary = "temporary";

	/**
	 * 
	 * @description：获取一条实习记录的路径
	 * @param practice
	 * @return：
	 */
	public static String getTestifyPath(Student student,Practice practice) {
		//获取学生所在班级、年级、专业、学院、学校的名字和id
		String classNameAndclassGradeId = student.getClassGrade().getClassGrade()+"_"+student.getClassGrade().getClassGradeId();
		String gradeAndgradeId = student.getClassGrade().getGrade().getGrade()+"_"+student.getClassGrade().getGrade().getGradeId();
		String specialtyNameAndspecialtyId=student.getClassGrade().getGrade().getSpecialty().getSpecialtyName()+"_"+student.getClassGrade().getGrade().getSpecialty().getSpecialtyId();
		String collegeNameAndcollegeId = student.getClassGrade().getGrade().getSpecialty().getCollege().getCollegeName()+"_"+student.getClassGrade().getGrade().getSpecialty().getCollege().getCollegeId();
		String schoolNameAndschoolId = student.getClassGrade().getGrade().getSpecialty().getCollege().getSchool().getSchoolName()+"_"+student.getClassGrade().getGrade().getSpecialty().getCollege().getSchool().getSchoolId();
				
		String studentTestifyPath = 
				schoolNameAndschoolId+"/" +
				collegeNameAndcollegeId+"/" +
				specialtyNameAndspecialtyId+"/" +
				gradeAndgradeId+"/" +
				classNameAndclassGradeId+"/" +
				student.getSno()+"/"+
				PracticeTestifyUtil.testifyPath+"/"+
				practice.getPracticeId()+"/";
		return studentTestifyPath;
	}/**
	 * 
	 * @description：获取该学生实习记录临时文件夹的路径
	 * @param practice
	 * @return：
	 */
	public static String getTemporaryPath(Student student) {
		//获取学生所在班级、年级、专业、学院、学校的名字和id
		String classNameAndclassGradeId = student.getClassGrade().getClassGrade()+"_"+student.getClassGrade().getClassGradeId();
		String gradeAndgradeId = student.getClassGrade().getGrade().getGrade()+"_"+student.getClassGrade().getGrade().getGradeId();
		String specialtyNameAndspecialtyId=student.getClassGrade().getGrade().getSpecialty().getSpecialtyName()+"_"+student.getClassGrade().getGrade().getSpecialty().getSpecialtyId();
		String collegeNameAndcollegeId = student.getClassGrade().getGrade().getSpecialty().getCollege().getCollegeName()+"_"+student.getClassGrade().getGrade().getSpecialty().getCollege().getCollegeId();
		String schoolNameAndschoolId = student.getClassGrade().getGrade().getSpecialty().getCollege().getSchool().getSchoolName()+"_"+student.getClassGrade().getGrade().getSpecialty().getCollege().getSchool().getSchoolId();
				
		String studentTemporaryPath = 
				schoolNameAndschoolId+"/" +
				collegeNameAndcollegeId+"/" +
				specialtyNameAndspecialtyId+"/" +
				gradeAndgradeId+"/" +
				classNameAndclassGradeId+"/" +
				student.getSno()+"/"+
				temporary+"/";
		return studentTemporaryPath;
	}
	/**
	 * 
	 * @description：清空指定文件夹下的所有文件
	 * @param path：实习记录证明相对路径
	 * @throws IOException 
	 */
	public static void deleteAllDirectorysFile(HttpServletRequest request,String path) throws IOException {
		File file = new File(request.getServletContext().getRealPath("/")+"/"+path);
		String[] list = file.list();
		for(String f:list) {
			System.gc();
			Files.delete(new File(request.getServletContext().getRealPath("/")+"/"+path+"/"+f).toPath());
//			new File(path+f).delete();
		}
	}
	/**
	 * 
	 * @description：删除指定文件夹
	 * @param path：实习记录证明相对路径
	 */
	public static void deleteAllDirectory(HttpServletRequest request,String path) {
		File file = new File(request.getServletContext().getRealPath("/")+"/"+path);
		String[] list = file.list();
		for(String f:list) {
			new File(path+f).delete();
		}
		file.delete();
	}
	/**
	 * 
	 * @author: wsc
	 * @date:2020年6月16日 下午9:52:02
	 * @description：删除指定文件
	 * @param file	文件的相对路径
	 * @param request：
	 */
	public static void deleteOneTestify(String filePath,HttpServletRequest request) {
		File file = new File(request.getServletContext().getRealPath("/")+"/"+filePath);
		file.delete();
	}
	/**
	 * 
	 * @author: wsc
	 * @date:2020年6月16日 下午9:52:02
	 * @description：删除指定文件
	 * @param file	文件的绝对路径
	 * @param request：
	 */
	public static void deleteAbsolutelyTestify(String filePath) {
		File file = new File(filePath);
		file.delete();
	}
}
